package com.ymttest.database.sqlwapper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtCouponScenario;
import com.ymttest.database.model.YmtCouponScenarioExample;
import com.ymttest.utils.EnvSetup;

public class YmtCouponScenarioWapper {
    SqlManager manage;

    public YmtCouponScenarioWapper() {
        manage = new SqlManager("ymt" + EnvSetup.getEnv());
    }

    /**
     * 插入优惠券场景
     * 
     * @param UserType
     *        0-全部用户 1-新用户 2-老用户
     * @param SellerId
     *        卖家ID
     * @param Specificproducts
     *        特殊产品ID
     * @param UsePlatforms
     *        使用平台 0-未知 1-YMTPC 2-YMTWAP 3-YMTAPP 4-SHANGOUAPP 5-SHANGOUWAP
     *        6-SHANGOUPC
     * @return
     */
    public int insertOneCouponScenario(int UserType, String SellerId, String Specificproducts, String UsePlatforms) {
        YmtCouponScenario data = new YmtCouponScenario();
        // 0-全部用户 1-新用户 2-老用户
        data.setIusertype(UserType);
        // 活动ID
        data.setSactivityids(null);
        // 物流类型
        data.setSlogisticstypes(null);
        // 产品品牌
        data.getSproductbrands();
        // 产品类别
        data.setSproductcategories(null);
        // 卖家ID
        data.setSsellerids(SellerId);
        // 特殊产品ID
        data.setSspecificproducts(Specificproducts);
        // 库存类型
        data.setSstockstatus(null);
        // 使用平台
        data.setSuseplatforms(UsePlatforms);
        // 用户等级
        data.setSuserlevels("0");
        int i = manage.insert("com.ymttest.database.mapping.YmtCouponScenarioMapper.insertSelective", data);
        manage.commit();
        return i;
    }

    /**
     * 查询最后一条优惠券场景ID
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public Integer getLastCouponScenarioId() {
        ArrayList<YmtCouponScenario> list = new ArrayList<YmtCouponScenario>();
        YmtCouponScenarioExample data = new YmtCouponScenarioExample();
        data.setDistinct(false);
        data.setOrderByClause("iCouponScenarioId desc");
        list = (ArrayList<YmtCouponScenario>) manage.getSelectResults(
                "com.ymttest.database.mapping.YmtCouponScenarioMapper.selectByExample", data);
        manage.commit();
        return list.get(0).getIcouponscenarioid();
    }

    public List<YmtCouponScenario> selectByiCouponScenarioId(int icouponscenarioid) {
        List<YmtCouponScenario> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponScenarioMapper.selectByPrimaryKey",
                        icouponscenarioid);
        return result;
    }

    public List<YmtCouponScenario> selectBysBatchCode(String sBatchCode) {
        List<YmtCouponScenario> result = manage
                .getSelectResults(
                        "com.ymttest.database.mapping.YmtCouponScenarioMapper.selectBySBatchCode",
                        sBatchCode);
        return result;
    }

    public void setiUserType(int iUserType, int iCouponScenarioId) {
        Map m = new HashMap();
        m.put("iUserType", iUserType);
        m.put("iCouponScenarioId", iCouponScenarioId);
        manage
                .update(
                        "com.ymttest.database.mapping.YmtCouponScenarioMapper.setiUserType",
                        m);
        manage.commit();
    }

    public void setsUserLevels(String sUserLevels, int iCouponScenarioId) {
        Map m = new HashMap();
        m.put("sUserLevels", sUserLevels);
        m.put("iCouponScenarioId", iCouponScenarioId);
        manage
                .update(
                        "com.ymttest.database.mapping.YmtCouponScenarioMapper.setsUserLevels",
                        m);
        manage.commit();
    }


    public static void main(String[] args) {
        YmtCouponScenarioWapper test = new YmtCouponScenarioWapper();
        // 插入优惠券场景
        // test.insertOneCouponScenario(0,null,null,"1,2");
        // 查询
        test.getLastCouponScenarioId();
    }
}
